package com.yp.shopping.service.Impl;

import com.yp.shopping.bean.GetResult;
import com.yp.shopping.bean.Result;
import com.yp.shopping.dao.CategoryMapper;
import com.yp.shopping.dao.GoodsCategoryMapper;
import com.yp.shopping.dao.GoodsMapper;
import com.yp.shopping.model.*;
import com.yp.shopping.service.GoodsService;
import com.yp.shopping.vo.GoodsV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    public List<GoodsV> getAllGoods() {

        GoodsExample goodsE = new GoodsExample();
        goodsE.or().andIdIsNotNull();
        List<Goods> goods = goodsMapper.selectByExample(goodsE);

        List<GoodsV> goodsV = getGoodsV(goods);

        return goodsV;
    }

    public GoodsV getGoodsById(Integer id) {

        Goods goods = goodsMapper.selectByPrimaryKey(id);

        List<Goods> goods1 = new ArrayList<Goods>();
        goods1.add(goods);

        List<GoodsV> goodsV = getGoodsV(goods1);

        return goodsV.get(0);
    }

    public Result deleteGoodsById(Integer id) {

        goodsMapper.deleteByPrimaryKey(id);

        GoodsCategoryExample goodsCategoryE = new GoodsCategoryExample();
        goodsCategoryE.or().andGoodsIdEqualTo(id);
        goodsCategoryMapper.deleteByExample(goodsCategoryE);

        return GetResult.success();
    }

    @Transactional
    public Result saveOrUpdateGoods(GoodsV goodsV) {

        Category category = goodsV.getCategory();
        Goods goods = goodsV.getGoods();

        if (goods.getId() == 0 || goods.getId() == null) {

            goodsMapper.insert(goodsV.getGoods());

            GoodsExample goodsE = new GoodsExample();
            goodsE.or().andNameEqualTo(goods.getName());
            List<Goods> goods1 = goodsMapper.selectByExample(goodsE);
            goods.setId(goods1.get(0).getId());

        }else {

            goodsMapper.updateByPrimaryKey(goods);
        }

        if (category.getId() == 0 || category.getId() == null) {

            categoryMapper.insert(category);

            CategoryExample categoryE = new CategoryExample();
            categoryE.or().andNameEqualTo(category.getName());
            List<Category> categories = categoryMapper.selectByExample(categoryE);
            category.setId(categories.get(0).getId());
        }

        GoodsCategory goodsCategory = new GoodsCategory();
        goodsCategory.setCategoryId(category.getId());
        goodsCategory.setGoodsId(goodsV.getGoods().getId());

        goodsCategoryMapper.insert(goodsCategory);

        return GetResult.success();
    }

    private List<GoodsV> getGoodsV(List<Goods> goods) {

        List<GoodsV> goodsV = new ArrayList<GoodsV>();
        GoodsCategoryExample goodsCategoryE = new GoodsCategoryExample();

        for (Goods g: goods
                ) {

            goodsCategoryE.clear();
            goodsCategoryE.or().andGoodsIdEqualTo(g.getId());
            List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectByExample(goodsCategoryE);
            Category category = categoryMapper.selectByPrimaryKey(goodsCategories.get(0).getCategoryId());

            GoodsV gv = new GoodsV();
            gv.setGoods(g);
            gv.setCategory(category);

            goodsV.add(gv);
        }

        return goodsV;
    }
}
